package testGridTree.action;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import testGridTree.GridTreeDao;
import testGridTree.GridTreeUtil;

/**
 * 演示懒加载表格树:第一次显示执行的action,也就是initLazy.
 * 用途是:只查询出来应该显示的第一层的节点.
 * connect me:419723443@qq.com
 */
public class InitTableTreeLazyAction extends Action {
	// 每页行数
	private static int DEFAULT_PAGE_SIZE = 10;
	/**
	 * 获取类型的下拉菜单数据
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 */
	  public ActionForward execute(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) {
			GridTreeDao dao = new GridTreeDao();
		List list = new ArrayList();
		// 结果行数
		int totalNum = 0;
		int[] rowStartEnd;
		int len = 0;
		try {
			totalNum = dao.getFirstLevelCount();
			/** 下面的三步都是使用的自己的类,用来设置一些在json要使用一些分页信息 */
			// 调用工具类的方法计算起始行和终止行（为前开和后开的）。
			rowStartEnd = GridTreeUtil.getStartAndEndInfo(request, totalNum,
					DEFAULT_PAGE_SIZE);
			list = dao.getList(rowStartEnd[0], rowStartEnd[1]);

			// 调用工具类的方法得到json字符串。
			String jsonStr = GridTreeUtil.getJsonStr(list, request);
			response.setContentType("text/html; charset=UTF-8");
			System.out.println("json串:" + jsonStr);
			PrintWriter out = response.getWriter();
			out.println(jsonStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
}
